﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

using System.Text;
using Microsoft.ApplicationBlocks.Data;
using Module.EntitiesInfo;

namespace Module.DataAccess
{
   public class CMSVideoClipDB
    {
       #region ConnectionString
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        public CMSVideoClipDB()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["localsql"].ToString();
        }
        #endregion

        #region Methods

        public virtual int Insert(CMSVideoClipInfo cMSVideoClipInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Title", cMSVideoClipInfo.Title),
				new SqlParameter("@Teaser", cMSVideoClipInfo.Teaser),
				new SqlParameter("@Body", cMSVideoClipInfo.Body),
				new SqlParameter("@Status", cMSVideoClipInfo.Status),
				new SqlParameter("@ShowType", cMSVideoClipInfo.ShowType),
				new SqlParameter("@HitCount", cMSVideoClipInfo.HitCount),
				new SqlParameter("@CreatedOn", cMSVideoClipInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSVideoClipInfo.CreatedBy),
				new SqlParameter("@Avatar", cMSVideoClipInfo.Avatar),
				new SqlParameter("@BigAvatar", cMSVideoClipInfo.BigAvatar),
				new SqlParameter("@ZoneId", cMSVideoClipInfo.ZoneId),
				new SqlParameter("@UrlVideo", cMSVideoClipInfo.UrlVideo)
			};

            return (int)SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "CMS_VideoClip_Insert", parameters);
        }

       
        public virtual void Update(CMSVideoClipInfo cMSVideoClipInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@VideoId", cMSVideoClipInfo.VideoId),
				new SqlParameter("@Title", cMSVideoClipInfo.Title),
				new SqlParameter("@Teaser", cMSVideoClipInfo.Teaser),
				new SqlParameter("@Body", cMSVideoClipInfo.Body),				
				new SqlParameter("@ShowType", cMSVideoClipInfo.ShowType),				
				new SqlParameter("@CreatedOn", cMSVideoClipInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSVideoClipInfo.CreatedBy),
				new SqlParameter("@Avatar", cMSVideoClipInfo.Avatar),				
				new SqlParameter("@ZoneId", cMSVideoClipInfo.ZoneId),
				new SqlParameter("@UrlVideo", cMSVideoClipInfo.UrlVideo)
			};

            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_VideoClip_Update", parameters);
        }

     
        public virtual void Delete(int videoId)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@VideoId", videoId)
			};

            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_VideoClip_Delete", parameters);
        }      

        public IDataReader Select(int? type, string para_1, string para_2)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_VideoClip_Select", type, para_1, para_2);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public IDataReader Select(int? type, string para_1)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_VideoClip_Select", type, para_1);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public IDataReader Select(int? type)
        {
            try
            {
                return SqlHelper.ExecuteReader(ConnectionString, "CMS_VideoClip_Select", type);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable SelectTbl(int? type, string para_1, string para_2)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_VideoClip_Select", type, para_1, para_2).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable SelectTbl(int? type, string para_1)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_VideoClip_Select", type, para_1).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable SelectTbl(int? type)
        {
            try
            {
                return SqlHelper.ExecuteDataset(ConnectionString, "CMS_VideoClip_Select", type).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetTop()
        {
            try
            {
                string sql = " SELECT top 100 * FROM CMS_VideoClip Order by CreatedOn desc";
                return SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text,sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Tính tổng số bản ghi
        /// </summary>
        /// <param name="keysearch">Từ khóa tìm kiếm</param>
        /// <param name="categoryId">Thể loại câu hỏi</param>
        /// <param name="starDatetime">Ngày bắt đầu</param>
        /// <param name="endDatetime">Ngày kết thúc</param>
        /// <param name="beginRow">Bản ghi đầu tiền</param>
        /// <param name="endRow">Bản ghi cuối</param>
        /// <returns></returns>
        public virtual int GetCount(string keysearch, int zoneId,int status, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                          
               new SqlParameter("@keysearch", keysearch),  
               new SqlParameter("@ZoneId",zoneId),   
               new SqlParameter("@Status",status),   
               new SqlParameter("@starDatetime", starDatetime),
               new SqlParameter("@endDatetime", endDatetime),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "CMS_VideoClip_Search", parameters);
        }

        /// <summary>
        /// Tìm kiếm
        /// </summary>
        /// <param name="keysearch">Từ khóa tìm kiếm</param>
        /// <param name="categoryId">Thể loại câu hỏi</param>
        /// <param name="starDatetime">Ngày bắt đầu</param>
        /// <param name="endDatetime">Ngày kết thúc</param>
        /// <param name="beginRow">Bản ghi đầu</param>
        /// <param name="endRow">Bản ghi cuối</param>
        /// <param name="orderByColumn">Sắp xếp</param>
        /// <returns></returns>
        public virtual DataTable Search(string keysearch, int zoneId,int status, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                
                new SqlParameter("@Keysearch", keysearch),  
                new SqlParameter("@ZoneId",zoneId), 
                new SqlParameter("@Status",status), 
                new SqlParameter("@StarDatetime", starDatetime),
                new SqlParameter("@EndDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@IsTotalRow", 0)
			};

            return SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "CMS_VideoClip_Search", parameters).Tables[0];
        }

        //Update Trạng thái 
        public virtual void UpdateStatus(int videoId, int status, int param)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@VideoID", videoId),
                new SqlParameter("@Status", status),
                new SqlParameter("@Param", param),
			};

            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "CMS_VideoClip_UpdateStatus", parameters);
        }


        #endregion
    }
}
